﻿// <copyright file="PostExtensions.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Core.Web.Extensions
{
    using System;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;

    using Blog.Data.Entities;

    public static class PostExtensions
    {
        public static MvcHtmlString ActionPostLink(this HtmlHelper hh, BlogEntry p)
        {
            return hh.ActionLink(p.Header, "Entry", "Blog",
                                 new { id = p.Id, name = p.HeaderUrl, area = "" },
                                 new { title = p.Header });
        }

        public static MvcHtmlString PostLink(this HtmlHelper helper, BlogEntry page, object htmlAttributes)
        {
            return PostLink(helper, null, page, htmlAttributes);
        }

        public static MvcHtmlString PostLink(this HtmlHelper helper, string linkText, BlogEntry page, object htmlAttributes)
        {
            return helper.ActionLink(linkText ?? page.Header, "Entry", "Blog", new { area = "", id = page.Id, name = page.HeaderUrl }, htmlAttributes);
        }

        public static string PostUrl(this UrlHelper helper, BlogEntry page)
        {
            var context = helper.RequestContext.HttpContext;
            return Utils.GetBaseUrl(context.Request.ApplicationPath, context.Request.Url, false) + helper.Action("Entry", "Blog", new { area = "", id = page.Id, name = page.HeaderUrl });
        }

        //TODO: Merge Post.PostSiteUrl and Page.SiteUrl
        public static string PostSiteUrl(this UrlHelper helper)
        {
            var context = helper.RequestContext.HttpContext;
            return Utils.GetBaseUrl(context.Request.ApplicationPath, context.Request.Url, true);
        }

        public static string PostLink(this HtmlHelper helper, BlogEntry page)
        {
            if (null != page)
                return page.SiteUrl();
            return string.Empty;
        }

        public static bool IsDefaultPost(this HtmlHelper helper)
        {
            return
                "Home".Equals(helper.ViewContext.RouteData.GetRequiredString("controller"), StringComparison.InvariantCultureIgnoreCase) &&
                "Default".Equals(helper.ViewContext.RouteData.GetRequiredString("action"), StringComparison.InvariantCultureIgnoreCase);
        }
    }
}